package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdCanteenBalanceService;
import com.ruoyi.business.service.IWsdCanteensService;
import com.ruoyi.business.service.IWsdPaymentRecordsService;
import com.ruoyi.business.service.IWsdRechargeRefundService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.WsdCanteensDTO;
import com.ruoyi.common.core.domain.dto.WsdCanteensOrderDTO;
import com.ruoyi.common.core.domain.dto.WsdOrderImportDTO;
import com.ruoyi.common.core.domain.dto.WsdStudentImportDTO;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.domain.table.OrderFoodTable;
import com.ruoyi.common.core.domain.table.OrderFoodVOTable;
import com.ruoyi.common.core.domain.vo.OrderPeopleNumVO;
import com.ruoyi.common.core.domain.vo.RechargeRefundVO;
import com.ruoyi.common.core.domain.vo.WsdCanteensOrderVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.date.DateTimeUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 饭堂信息Controller
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Slf4j
@RestController
@RequestMapping("wsd_canteens")
public class WsdCanteensController extends BaseController {

    @Autowired
    private IWsdCanteensService wsdCanteensService;

    @Autowired
    private IWsdRechargeRefundService wsdRechargeRefundService;
    @Autowired
    private IWsdPaymentRecordsService wsdPaymentRecordsService;
    @Autowired
    private IWsdCanteenBalanceService wsdCanteenBalanceService;
    /**
     * 查询饭堂信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:list')")
    @GetMapping("/list")
    public TableDataInfo list(WsdCanteens wsdCanteens) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdCanteens.setSchoolId(schoolId);
        }
        startPage();
        List<WsdCanteens> list = wsdCanteensService.selectWsdCanteensList(wsdCanteens);
        return getDataTable(list);
    }

    /**
     * 导出饭堂信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:export')")
    @Log(title = "饭堂信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdCanteens wsdCanteens)
    {
        List<WsdCanteens> list = wsdCanteensService.selectWsdCanteensList(wsdCanteens);
        ExcelUtil<WsdCanteens> util = new ExcelUtil<WsdCanteens>(WsdCanteens.class);
        util.exportExcel(response, list, "饭堂信息数据");
    }

    /**
     * 获取饭堂信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wsdCanteensService.selectWsdCanteensById(id));
    }

    /**
     * 新增饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:add')")
    @Log(title = "饭堂信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdCanteensDTO wsdCanteensDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        wsdCanteensDTO.setSchoolId(schoolId);
        return toAjax(wsdCanteensService.insertWsdCanteens(wsdCanteensDTO));
    }

    /**
     * 修改饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:edit')")
    @Log(title = "饭堂信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdCanteensDTO wsdCanteensDTO) {
        return toAjax(wsdCanteensService.updateWsdCanteens(wsdCanteensDTO));
    }

    /**
     * 删除饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:remove')")
    @Log(title = "饭堂信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wsdCanteensService.deleteWsdCanteensByIds(ids));
    }
    /**
     * 饭堂订餐管理
     */
    @GetMapping("/orderFoodList")
    public TableDataInfo orderFoodList(WsdCanteensOrderDTO wsdCanteensOrderDTO) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdCanteensOrderDTO.setSchoolId(schoolId);
        }
        startPage();
        List<WsdCanteensOrderVO> list = wsdCanteensService.orderFoodList(wsdCanteensOrderDTO);
        return getDataTable(list);
    }

    /**
     * 获取饭堂订餐详细信息
     */
    @GetMapping(value = "/getOrderFood/{paymentId}")
    public AjaxResult getOrderFood(@PathVariable("paymentId") Long paymentId)
    {
        //缴费项目
        List<OrderFoodVOTable> orderFoodVOTableList = wsdPaymentRecordsService.selectWsdPaymentRecordsByPaymentId(paymentId);
        return success(orderFoodVOTableList);
    }

    /**
     * 订餐人数
     * @param wsdCanteensOrderDTO
     * @return
     */
    @GetMapping("/orderPeopleNum")
    public AjaxResult orderPeopleNum(WsdCanteensOrderDTO wsdCanteensOrderDTO) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdCanteensOrderDTO.setSchoolId(schoolId);
        }
        OrderPeopleNumVO orderPeopleNumVO = wsdCanteenBalanceService.orderPeopleNum(wsdCanteensOrderDTO);
        return success(orderPeopleNumVO);
    }

    /**
     * 新增饭堂订餐信息
     * @param wsdCanteensOrderDTO
     * @return
     */
    @PostMapping("/addOrdering")
    public AjaxResult addOrdering(@RequestBody WsdCanteensOrderDTO wsdCanteensOrderDTO) {
        AjaxResult ajaxResult = wsdCanteensService.addOrdering(wsdCanteensOrderDTO);
        return ajaxResult;
    }
    /**
     * 导入订餐信息
     */
    @PostMapping("/importOrdering")
    @ResponseBody
    public AjaxResult importOrdering(@RequestParam("file") MultipartFile file) throws Exception
    {
//        if (classId==null){
//            return AjaxResult.error( "班级信息为空，请选择班级后重新上传！！！" );
//        }
//        if (gradeId==null){
//            return AjaxResult.error( "年级信息为空，请选择班级后重新上传！！！" );
//        }
        if (file.isEmpty()){
            return AjaxResult.error( "文件为空，请重新上传！！！" );
        }
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            return AjaxResult.error( "文件解析失败，请重新上传！！！" );
        }
        ExcelUtil<WsdOrderImportDTO> util = new ExcelUtil<WsdOrderImportDTO>(WsdOrderImportDTO.class);
        List<WsdOrderImportDTO> wsdOrderImportDTOList = util.importExcel(inputStream);
        Long classId = 0L;
        Long gradeId = 0L;
        AjaxResult result = wsdCanteensService.importOrdering(wsdOrderImportDTOList, classId, gradeId);
        return result;
    }
    /**
     * 导出
     */
    @PostMapping("/exportOrder")
    public void exportOrder(HttpServletResponse response, WsdCanteensOrderDTO wsdCanteensOrderDTO) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdCanteensOrderDTO.setSchoolId(schoolId);
        }
        List<WsdCanteensOrderVO> list = wsdCanteensService.orderFoodList(wsdCanteensOrderDTO);
        ExcelUtil<WsdCanteensOrderVO> util = new ExcelUtil<WsdCanteensOrderVO>(WsdCanteensOrderVO.class);
        util.exportExcel(response, list, "订餐管理");
    }
}
